package barcode.android.dao.impl;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.UserInfoDao;
import barcode.android.model.Dorm;
import barcode.android.model.UserInfo;
import android.test.ActivityInstrumentationTestCase2;

public class UserInfoDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public UserInfoDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddUserInfo() {
		UserInfo info = new UserInfo();
		info.setUid("U200917829");
		info.setName("hello");
		info.setPassword("world");
		info.setEmail("dsa@dd.com");
		info.setQq("213321");
		info.setPhone("12324234");
		info.setAbstracts("活动详情");
		Dorm dorm = new Dorm();
		dorm.setName("韵苑16#");
		info.setDorm(dorm);

		UserInfoDao dao = new UserInfoDaoImpl();
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			dao.add(info);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
	}

	public void testUpdateUserInfo() {
		UserInfo info = new UserInfo();
		info.setId(1);
		info.setUid("U200917833");
		info.setName("baidu");
		info.setPassword("tencent");
		info.setEmail("dfdsa@qq.com");
		info.setQq("193847");
		info.setPhone("13433333333");
		info.setAbstracts("详情描述");
		Dorm dorm = new Dorm();
		dorm.setId(2);
		info.setDorm(dorm);

		UserInfoDao dao = new UserInfoDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.update(info);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testDeleteUser() {
		UserInfoDao dao = new UserInfoDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.delete();
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testQueryUser() {
		UserInfoDao dao = new UserInfoDaoImpl();
		UserInfo res = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.query();
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(res.getId(), 1);
		Assert.assertEquals(res.getUid(), "U200917833");
		Assert.assertEquals(res.getName(), "baidu");
		Assert.assertEquals(res.getPassword(), "tencent");
		Assert.assertNotNull(res);
	}
}
